<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Jessibuca Pro MSE 5x5 demo</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <script src="./js/jessibuca-pro-demo.js"></script>
    <style>
        .root {
            display: flex;
            place-content: center;
            margin-top: 3rem;
        }

        .container-shell {
            backdrop-filter: blur(5px);
            background: hsla(0, 0%, 50%, 0.5);
            padding: 30px 4px 10px 4px;
            /* border: 2px solid black; */
            width: auto;
            position: relative;
            border-radius: 5px;
            box-shadow: 0 10px 20px;
        }

        .container-shell:before {
            content: "jessibuca Pro MSE demo player";
            position: absolute;
            color: darkgray;
            top: 4px;
            left: 10px;
            text-shadow: 1px 1px black;
        }

        .container {
            background: rgba(13, 14, 27, 0.7);
            width: 320px;
            height: 199px;
            display: inline-block;
            margin-right: 10px;
            margin-bottom: 10px;
        }

        #container {

        }

        .input {
            display: flex;
            margin-top: 10px;
            color: white;
            place-content: stretch;
        }

        .input2 {
            bottom: 0px;
        }

        .input input {
            flex: auto;
        }

        .err {
            position: absolute;
            top: 40px;
            left: 10px;
            color: red;
        }

        .option {
            position: absolute;
            top: 4px;
            right: 10px;
            display: flex;
            place-content: center;
            font-size: 12px;
        }

        .option span {
            color: white;
        }

        .page {
            background: url('http://jessibuca.monibuca.com/bg.jpg');
            background-repeat: no-repeat;
            background-position: top;
        }

        @media (max-width: 720px) {
            .container {
                width: 90vw;
                height: 52.7vw;
            }
        }
    </style>
</head>
<body class="page">
<div class="root">

    <div class="container-shell">
        <div>
            <div class="container" id="container1"></div>
            <div class="container" id="container2"></div>
            <div class="container" id="container3"></div>
            <div class="container" id="container4"></div>
            <div class="container" id="container5"></div>
        </div>
        <div>
            <div class="container" id="container6"></div>
            <div class="container" id="container7"></div>
            <div class="container" id="container8"></div>
            <div class="container" id="container9"></div>
            <div class="container" id="container10"></div>
        </div>
        <div>
            <div class="container" id="container11"></div>
            <div class="container" id="container12"></div>
            <div class="container" id="container13"></div>
            <div class="container" id="container14"></div>
            <div class="container" id="container15"></div>
        </div>
        <div>
            <div class="container" id="container16"></div>
            <div class="container" id="container17"></div>
            <div class="container" id="container18"></div>
            <div class="container" id="container19"></div>
            <div class="container" id="container20"></div>
        </div>
        <div>
            <div class="container" id="container21"></div>
            <div class="container" id="container22"></div>
            <div class="container" id="container23"></div>
            <div class="container" id="container24"></div>
            <div class="container" id="container25"></div>
        </div>
        <div class="input">
            <div>
                当前浏览器：<span id="mseSupport" style="color: green;display: none">支持MSE H265解码</span>
                <span id="mseNotSupport" style="color: red;display: none">不支持MSE H265解码,会自动切换成wasm解码</span>
            </div>
        </div>
        <div class="input" style="width: 1000px;flex-direction: column">
            <div>
                js为单线程运行时, 无法通过多线程提升并发性能.
            </div>
            <div>将音视频解码在worker中进行, 能有效利用多核处理器提升多画面播放的流畅度.</div>
            <div>
                chrome限制同源http请求最多6个并发, 因此超过6个并发请求我们采用websocket-flv来播放
            </div>
            <div>请准备M7S服务端来测试. worker js不能跨域调用,</div>
        </div>
        <div class="input">
            <div>
                <input
                    onclick="replay()"
                    type="checkbox"
                    id="isFlv"
                /><span>是否flv格式</span>
                <input
                    checked
                    onclick="replay()"
                    type="checkbox"
                    id="demuxUseWorker"
                /><span>硬解码(MediaSource，Webcodec)worker解封装</span>
                <input
                    checked
                    onclick="replay()"
                    type="checkbox"
                    id="mseDecoderUseWorker"
                /><span>硬解码(MediaSource)worker解码</span>
            </div>
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl1"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl2"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl3"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl4"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl5"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl6"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl7"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl8"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl9"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl10"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl11"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl12"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl13"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl14"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl15"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl16"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl17"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl18"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl19"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl20"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl21"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl22"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl23"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl24"
                value=""
            />
        </div>
        <div class="input">
            <div>输入URL：</div>
            <input
                autocomplete="on"
                id="playUrl25"
                value=""
            />
        </div>

        <div class="input">
            <button id="play">播放</button>
            <button id="pause" style="display: none">停止</button>
        </div>
        <div class="input" style="line-height: 30px">
            <button id="destroy">销毁</button>
        </div>
    </div>
</div>

<script>
    var $player = document.getElementById('play');
    var $pause = document.getElementById('pause');
    var $destroy = document.getElementById('destroy');
    var $demuxUseWorker = document.getElementById('demuxUseWorker');
    var $mseDecoderUseWorker = document.getElementById('mseDecoderUseWorker');
    var $isFlv = document.getElementById('isFlv');


    var showOperateBtns = true; // 是否显示按钮
    var forceNoOffscreen = true; //
    var playList = [];
    var num = 25;

    function _create(id, index) {
        var $container = document.getElementById('container' + id);

        var jessibuca = new JessibucaPro({
            container: $container,
            decoder: './js/decoder-pro.js',
            videoBuffer: Number($videoBuffer.value), // 缓存时长
            videoBufferDelay: Number($videoBufferDelay.value), //
            isResize: false,
            text: "",
            loadingText: "加载中",
            debug: false,
            debugLevel: "debug",
            decoderErrorAutoWasm: false,
            showBandwidth: showOperateBtns, // 显示网速
            showPerformance: showOperateBtns,
            useMSE: true,
            isFlv: $isFlv.checked === true,
            controlAutoHide: true,
            operateBtns: {
                fullscreen: showOperateBtns,
                screenshot: showOperateBtns,
                play: showOperateBtns,
                audio: showOperateBtns,
            },
            watermarkConfig: {
                text: {
                    content: 'jessibuca-pro'
                },
                right: 10,
                top: 10
            },
            demuxUseWorker: $demuxUseWorker.checked === true,
            mseDecoderUseWorker: $mseDecoderUseWorker.checked === true,
            websocketOpenTimeout: 3 + index * 0.5,
            loadingTimeout: 5 + index * 0.5,
        },);
        jessibuca.on(JessibucaPro.EVENTS.playFailedAndPaused, (error) => {
            jessibuca.showErrorMessageTips('播放异常：' + error);
        })

        jessibuca.on(JessibucaPro.EVENTS.crashLog, (log) => {
            console.error('crashLog', log)
        })

        playList.push(jessibuca);
    }


    function create() {
        for (let i = 0; i < num; i++) {
            _create(i + 1, i);
            $player.style.display = 'inline-block';
            $pause.style.display = 'none';
            $destroy.style.display = 'none';
        }
    }

    create();


    $player.addEventListener('click', function () {
        for (let i = 0; i < num; i++) {
            var id = i + 1;
            var $playHref = document.getElementById('playUrl' + id);
            let player = playList[i];
            if ($playHref.value) {
                setTimeout((url) => {
                    console.log(url);
                    player && player.play(url).then(() => {

                    }).catch((e) => {
                        console.error(e);
                    });
                }, 0, $playHref.value)
            }
        }


        $player.style.display = 'none';
        $pause.style.display = 'inline-block';
        $destroy.style.display = 'inline-block';
    }, false)


    $pause.addEventListener('click', function () {
        $player.style.display = 'inline-block';
        $pause.style.display = 'none';
        for (let i = 0; i < num; i++) {
            let player = playList[i];
            player && player.pause()
        }
    })

    function destroyAndCreate() {
        return new Promise((resolve, reject) => {
            const destroyList = [];
            for (let i = 0; i < num; i++) {
                let player = playList[i];
                if (player) {
                    destroyList.push(player.destroy());
                }
            }
            Promise.all(destroyList).then(() => {
                playList = [];
                setTimeout(() => {
                    create();
                    resolve();
                }, 100)
            }).catch((e) => {
                console.error(e);
            })
        })

    }


    $destroy.addEventListener('click', function () {
        destroyAndCreate().then(() => {

        })
    })

    function checkSupport() {
        return window.MediaSource && window.MediaSource.isTypeSupported('video/mp4; codecs="hev1.1.6.L123.b0"');
    }

    let support = document.getElementById('mseSupport');
    let notSupport = document.getElementById('mseNotSupport');
    if (checkSupport()) {
        support.style.display = 'inline-block'
    } else {
        notSupport.style.display = 'inline-block'
    }

</script>

</body>
</html>
